<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "set_user_up".
 *
 * @property int $id
 * @property float $vip1_price 零售价格
 * @property int $vip2_box
 * @property int $vip3_box
 * @property int $svip1_box
 * @property int $svip2_box
 * @property float $vip2_amount
 * @property float $vip3_amount
 * @property float $svip1_amount
 * @property float $svip2_amount
 * @property float $vip2_same 顾客同级奖
 * @property float $vip3_same vip同级奖
 * @property float $svip1_same 经销商推经销商
 * @property float $svip2_same 区代推区代
 * @property float $svip1_repeat 百分比, 经销商推经销商 复购
 * @property float $svip2_repeat 复购, 区代资金
 * @property int $box1
 * @property int $box2
 * @property int $box3
 * @property int $box4
 * @property float $box1_award
 * @property float $box2_award
 * @property float $box3_award
 * @property float $box4_award
 * @property int $address_num 区代分红条件, 直推3个区代
 * @property float $address_award 每单(还是盒) 拿30元
 * @property int $box_num 直推几个经销商
 * @property int $store_begin 默认进云仓数量, 从多少开始
 */
class SetUserUp extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_user_up';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['vip1_price', 'vip2_amount', 'vip3_amount', 'svip1_amount', 'svip2_amount', 'vip2_same', 'vip3_same', 'svip1_same', 'svip2_same', 'svip1_repeat', 'svip2_repeat', 'box1_award', 'box2_award', 'box3_award', 'box4_award', 'address_award'], 'number'],
            [['vip2_box', 'vip3_box', 'svip1_box', 'svip2_box', 'box1', 'box2', 'box3', 'box4', 'address_num'], 'integer'],

            [['box_num', 'store_begin'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'vip1_price' => 'Vip 1 Price',
            'vip2_box' => 'Vip 2 Box',
            'vip3_box' => 'Vip 3 Box',
            'svip1_box' => 'Svip 1 Box',
            'svip2_box' => 'Svip 2 Box',
            'vip2_amount' => 'Vip 2 Amount',
            'vip3_amount' => 'Vip 3 Amount',
            'svip1_amount' => 'Svip 1 Amount',
            'svip2_amount' => 'Svip 2 Amount',
            'vip2_same' => 'Vip 2 Same',
            'vip3_same' => 'Vip 3 Same',
            'svip1_same' => 'Svip 1 Same',
            'svip2_same' => 'Svip 2 Same',
            'svip1_repeat' => 'Svip 1 Repeat',
            'svip2_repeat' => 'Svip 2 Repeat',
            'box1' => 'Box 1',
            'box2' => 'Box 2',
            'box3' => 'Box 3',
            'box4' => 'Box 4',
            'box1_award' => 'Box 1 Award',
            'box2_award' => 'Box 2 Award',
            'box3_award' => 'Box 3 Award',
            'box4_award' => 'Box 4 Award',
            'address_num' => 'Address Num',
            'address_award' => 'Address Award',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetUserUp' => $data]) && $model->save()) {
            return true;
        }
    }
}
